package cn.servlet;

import cn.entity.*;
import cn.service.*;
import cn.service.impl.*;
import cn.util.Const;
import com.mysql.jdbc.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/orders")
public class OrdersServlet extends HttpServlet {
	OrdersService ordersService=new OrdersServiceImpl();
	GoodsService goodsService=new GoodsServiceImpl();
	UsersService usersService=new UsersServiceImpl();
	CartsService cartsService=new CartsServiceImpl();
	OrdersdetailService ordersdetailService=new OrdersdetailServiceImpl();
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrdersServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		switch(action){
		case "query":
			query(request,response);
			break;
		case "add":
			add(request,response);
			break;
		case "update":
			update(request,response);
			break;
		case "delete":
			delete(request,response);
			break;
		}
	}
	//查询
	protected void query(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Users users=(Users)request.getSession().getAttribute("users");
		List<Orders> list=ordersService.find(users.getId());
		for(Orders v:list){
			List<Ordersdetail> details=ordersdetailService.find(v.getId());
			for(Ordersdetail od:details){
				if(od.getGid()!=null){
					Goods goods=goodsService.selectByPrimaryKey(od.getGid());
					od.setGoods(goods);
				}
			}
			v.setDetails(details);
		}
		request.setAttribute("list", list);
		request.getRequestDispatcher("orders.jsp").forward(request, response);
	}
	
	
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//当前用户
		Users users=(Users)request.getSession().getAttribute("users");
		int uid=users.getId();
		//查购物车
		List<Carts> list=cartsService.find(uid);
		//总价格
		Double total=0.0;
		for(Carts v:list){
//			if(v.getGid()!=null){
//				Goods goods=goodsService.selectByPrimaryKey(v.getGid());
//				v.setGoods(goods);
//			}
			total+=v.getPrice();
		}
		Orders orders=new Orders();
		orders.setUid(users.getId());
		orders.setOrderno(Const.getNos());
		orders.setOptime(Const.getTime());
		orders.setPrice(total);
		orders.setStatus("未发货");
		orders.setIsdel(0);
		int n=ordersService.insert(orders);

		if(n>0){
			//获取oid
			int oid=ordersService.getMaxId();
			for(Carts v:list){
				Ordersdetail od=new Ordersdetail();
				od.setOid(oid);
				od.setUid(uid);
				od.setQuantity(v.getQuantity());
				od.setGid(v.getGid());
				od.setPrice(v.getPrice());
				int count=ordersdetailService.insert(od);
				//删除
				if(count>0){
					cartsService.deleteByPrimaryKey(v.getId());
				}
			}
		}
		response.sendRedirect(Const.ROOT+"orders?action=query");
//		int quantity=0;
//		if(!StringUtils.isNullOrEmpty(squantity)){
//			quantity=Integer.parseInt(squantity);
//		}
//		if(!StringUtils.isNullOrEmpty(sgid)){
//			Integer gid=Integer.parseInt(sgid);
//			Goods goods=goodsService.selectByPrimaryKey(gid);
//			Orders orders=new Orders();
//			orders.setGid(gid);
//			orders.setQuantity(quantity);
//			orders.setUid(users.getId());
//			orders.setPrice(goods.getPrice()*quantity);
//			int n=ordersService.insert(orders);
//			JSONObject obj=new JSONObject();
//			if(n>0){
//				obj.put("code", 200);
//				obj.put("msg", "ok");
//			}else{
//				obj.put("code", 500);
//				obj.put("msg", "error");
//			}
//			out.print(obj.toJSONString());
//		}
		
	}
	
	protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		String sid=request.getParameter("id");
		String status=request.getParameter("status");
		
		if(!StringUtils.isNullOrEmpty(sid)){
			Integer id=Integer.parseInt(sid);
			Orders orders=ordersService.selectByPrimaryKey(id);
			orders.setStatus(status);
			int n=ordersService.updateByPrimaryKey(orders);
			if(n>0){
				out.print("{\"code\":200,\"msg\":\"ok\"}");
			}else{
				out.print("{\"code\":500,\"msg\":\"error\"}");
			}
		}
		out.close();
	}

	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		String sid=request.getParameter("id");
		
		if(!StringUtils.isNullOrEmpty(sid)){
			Integer id=Integer.parseInt(sid);
			Orders orders=ordersService.selectByPrimaryKey(id);
			orders.setIsdel(1);
			int n=ordersService.updateByPrimaryKey(orders);
			if(n>0){
				out.print("{\"code\":200,\"msg\":\"ok\"}");
			}else{
				out.print("{\"code\":500,\"msg\":\"error\"}");
			}
		}
		out.close();
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}
}
